import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
import { LoginUsers, Users, materiel,treeList} from '../mockdata/user';
let _Users = Users;
let _materiel=materiel;
let _tree=treeList;

export default {
  /**
   * mock bootstrap
   */
  bootstrap() {
    let mock = new MockAdapter(axios);

    // mock success request
    mock.onGet('/success').reply(200, {
      msg: 'success'
    });

    // mock error request
    mock.onGet('/error').reply(500, {
      msg: 'failure'
    });

    //登录
    mock.onPost('/login').reply(config => {
      let {username, password} = JSON.parse(config.data);
      return new Promise((resolve, reject) => {
        let user = null;
        setTimeout(() => {
          let hasUser = LoginUsers.some(u => {
            if (u.username === username && u.password === password) {
              user = JSON.parse(JSON.stringify(u));
              user.password = undefined;
              return true;
            }
          });

          if (hasUser) {
            resolve([200, { code: 200, msg: '请求成功', user }]);
          } else {
            resolve([200, { code: 500, msg: '账号或密码错误' }]);
          }
        }, 500);
      });
    });

    //获取用户列表
    mock.onGet('/user/list').reply(config => {
      let {name} = config.params;
      let mockUsers = _Users.filter(user => {
        if (name && user.name.indexOf(name) == -1) return false;
        return true;
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            users: mockUsers
          }]);
        }, 500);
      });
    });

    //获取用户列表（分页）
    mock.onGet('/user/listpage').reply(config => {
      let {page, propCode} = config.params;
      let mockUsers = _Users.filter(user => {

        if (propCode && user.propCode.indexOf(propCode) == -1) return false;
        return true;
      });
      let total = mockUsers.length;

      mockUsers = mockUsers.filter((u, index) => index < 20 * page && index >= 20 * (page - 1));

      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            total: total,
            users: mockUsers
          }]);
        }, 500);
      });
    });

    //删除用户
    mock.onGet('/user/remove').reply(config => {
      let { id } = config.params;
      _Users = _Users.filter(u => u.id !== id);
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            code: 200,
            msg: '删除成功'
          }]);
        }, 500);
      });
    });

    //编辑用户
    mock.onGet('/user/edit').reply(config => {
      let { id, propCode, levelCode1, levelCode2, cateCode, cateName } = config.params;
      _Users.some(u => {
        if (u.id === id) {
          u.propCode = propCode;
          u.levelCode1 = levelCode1;
          u.levelCode2 = levelCode2;
          u.cateCode = cateCode;
          u.cateName = cateName;
          return true;
        }
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            code: 200,
            msg: '编辑成功'
          }]);
        }, 500);
      });
    });

    //新增用户
    mock.onGet('/user/add').reply(config => {
      let { propCode, levelCode1, levelCode2, cateCode, cateName } = config.params;
      _Users.push({
        propCode: propCode,
        levelCode1: levelCode1,
        levelCode2: levelCode2,
        cateCode: cateCode,
        cateName: cateName
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            code: 200,
            msg: '新增成功'
          }]);
        }, 500);
      });
    });

    //获取物料信息列表
      mock.onGet('/materiel/list').reply(config => {
          let {itemCode} = config.params;
          let mockMateriel = materiel.filter(materiel => {
              if (itemCode && materiel.itemCode.indexOf(itemCode) == -1) return false;
              return true;
          });
          return new Promise((resolve, reject) => {
              setTimeout(() => {
                  resolve([200, {
                      materiel: mockMateriel
                  }]);
              }, 500);
          });
      });
      //获取物料信息列表（分页）
      mock.onGet('/materiel/listpage').reply(config => {
          let {page, itemCode} = config.params;
          let mockMateriel = materiel.filter(materiel => {
              if (itemCode && materiel.itemCode.indexOf(itemCode) == -1) return false;
              return true;
          });
          let total = mockMateriel.length;
          mockMateriel = mockMateriel.filter((u, index) => index < 20 * page && index >= 20 * (page - 1));
          return new Promise((resolve, reject) => {
              setTimeout(() => {
                  resolve([200, {
                      total: total,
                      materiel: mockMateriel
                  }]);
              }, 500);
          });
      });
      //获取物料树列表信息
      mock.onGet('/tree/list').reply(config => {
          let {id} = config.params;

          let mockTree= _tree.filter(tree => {
              if (id && tree.id.indexOf(id) == -1) return false;
              return true;
          });
          return new Promise((resolve, reject) => {
              setTimeout(() => {
                  resolve([200, {
                      tree: mockTree
                  }]);
              }, 500);
          });
      });
      //删除用户
      mock.onGet('/materiel/remove').reply(config => {
          let { id } = config.params;
          _materiel = _materiel.filter(u => u.id !== id);
          return new Promise((resolve, reject) => {
              setTimeout(() => {
                  resolve([200, {
                      code: 200,
                      msg: '删除成功'
                  }]);
              }, 500);
          });
      });

      //编辑用户
      mock.onGet('/materiel/edit').reply(config => {

          let { id, itemCode, itemName, cateId, model, inflamingRetarding, shellStuff,remark} = config.params;
          _materiel.some(u => {
              if (u.id === id) {
                  u.itemCode = itemCode;
                  u.itemName = itemName;
                  u.cateId = cateId;
                  u.model = model;
                  u.inflamingRetarding = inflamingRetarding;
                  u.shellStuff = shellStuff;
                  u.remark = remark;
                  return true;
              }
          });
          return new Promise((resolve, reject) => {
              setTimeout(() => {
                  resolve([200, {
                      code: 200,
                      msg: '编辑成功'
                  }]);
              }, 500);
          });
      });

      //新增用户
      mock.onGet('/materiel/add').reply(config => {
          let {itemCode, itemName, cateId, model, inflamingRetarding, shellStuff,remark} = config.params;
          _materiel.push({

              itemCode: itemCode,
              itemName: itemName,
              cateId: cateId,
              model: model,
              inflamingRetarding: inflamingRetarding,
              shellStuff: shellStuff,
              remark: remark
          });
          return new Promise((resolve, reject) => {
              setTimeout(() => {
                  resolve([200, {
                      code: 200,
                      msg: '新增成功'
                  }]);
              }, 500);
          });
      });

  }
};
